package com.edgclub.edg.api;

import android.content.Context;
import android.util.Log;

import com.edgclub.edg.GuZhiApplication;
import com.edgclub.edg.constant.BBConstant;
import com.edgclub.edg.http.async.AsyncHttpClient;
import com.edgclub.edg.http.async.AsyncHttpResponseHandler;
import com.edgclub.edg.http.async.RequestParams;
import com.edgclub.edg.http.async.ResponseHandlerInterface;
import com.edgclub.edg.utils.util.CryptUtil;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * 上传图片
 * 
 * @author 陈坤彬
 *
 */
public class UploadApi extends GuZhiApi {
	private AsyncHttpClient client;
	public static final int DEFAULT_SOCKET_TIMEOUT = 60 * 1000;
	public UploadApi(Context mContext) {
		super(mContext);
		client = new AsyncHttpClient();
		client.setConnectTimeout(DEFAULT_SOCKET_TIMEOUT);
	}

	/**
	 * 上传图片
	 * 
	 * @param file
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
	public void beginUpload(File file, AsyncHttpResponseHandler fileHandler)
			throws FileNotFoundException {
		if (!file.exists()) {
			throw new FileNotFoundException("file:" + file.getName()
					+ " not exists...");
		}

		String time = String.valueOf(System.currentTimeMillis());
		RequestParams params = new RequestParams();
		params.put("t", time);
		params.put("partFile", file);

		params.put("sign", CryptUtil.md5("t=" + time + "&key=" + ApiConfig.HTTP_KEY));
		client.post(ApiConfig.BASE_URL + ApiConfig.APIURL.URL_UPLOADFILE,
				params, fileHandler);
	}

	/**
	 * 上传图片
	 * 
	 * @param file
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
	public void beginUploadTopicImage(File file,
			AsyncHttpResponseHandler fileHandler) throws FileNotFoundException {
		if (!file.exists()) {
			throw new FileNotFoundException("file:" + file.getName()
					+ " not exists...");
		}

		RequestParams params = new RequestParams();
		String time = String.valueOf(System.currentTimeMillis());
		params.put("t", time);
		params.put("logo", file);

		params.put("sign", CryptUtil.md5("t=" + time + "&key=" + ApiConfig.HTTP_KEY));

		client.post(
				ApiConfig.BASE_URL + ApiConfig.APIURL.URL_UPLOADTOPICIMAGE,
				params, fileHandler);
	}

	/**
	 * 上传帖子图片
	 * 
	 * @param file
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
	public void beginUploadTopicAlbum(File file, String topicId, String seq,String orib,
									  ResponseHandlerInterface fileHandler) throws FileNotFoundException {
		if (!file.exists()) {
			throw new FileNotFoundException("file:" + file.getName()
					+ " not exists...");
		}

		String ori="0";
		if(orib.equals("true")){
			ori="1";
		}
		String time = String.valueOf(System.currentTimeMillis());
		RequestParams params = new RequestParams();
		params.put("userId", GuZhiApplication.getInstance().getUserId());
		params.put("postsId", topicId);
		params.put("seq", seq);
		params.put(time+"_file", file);
		params.put("ori", ori);
		params.put(
				"sign",
				CryptUtil.sign(params,ApiConfig.HTTP_KEY));
		Log.d("url:", ApiConfig.BASE_URL + ApiConfig.APIURL.URL_UPLOADTOPICALBUM+params.toString());
		client.setTimeout(60 * 1000);
		client.post(
				ApiConfig.BASE_URL + ApiConfig.APIURL.URL_UPLOADTOPICALBUM,
				params, fileHandler);
	}

	/**
	 * 上传语音
	 *
	 * @param file
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
	public void beginUploadTopicVoice(File file, String topicId, String seq,int recodeTime,
									  AsyncHttpResponseHandler fileHandler) throws FileNotFoundException {
		if (!file.exists()) {
			throw new FileNotFoundException("file:" + file.getName()
					+ " not exists...");
		}
		RequestParams params = new RequestParams();
		String time = String.valueOf(System.currentTimeMillis());
		params.put("userId", GuZhiApplication.getInstance().getUserId());
		params.put("postsId", topicId);
		params.put("seq", seq);
		params.put("url", BBConstant.URL_VOICE_PROTOCOL+recodeTime);
		params.put(time+"_file", file);
		params.put(
				"sign",
				CryptUtil.sign(params,ApiConfig.HTTP_KEY));
		Log.d("url:", ApiConfig.BASE_URL + ApiConfig.APIURL.URl_POSTS_UPLOADVOICE+params.toString());
		client.post(
				ApiConfig.BASE_URL + ApiConfig.APIURL.URl_POSTS_UPLOADVOICE,
				params, fileHandler);
	}

	/**
	 * 上传回复图片
	 *
	 * @param file
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
	public void beginUploadReplyAlbum(File file, String topicId, String replyId, String seq,String orib,
									  ResponseHandlerInterface fileHandler) throws FileNotFoundException {
		if (!file.exists()) {
			throw new FileNotFoundException("file:" + file.getName()
					+ " not exists...");
		}

		String ori="0";
		if(orib.equals("true")){
			ori="1";
		}
		String time = String.valueOf(System.currentTimeMillis());
		RequestParams params = new RequestParams();
		params.put("userId", GuZhiApplication.getInstance().getUserId());
		params.put("postsId", topicId);
		params.put("replyId", replyId);
		params.put("seq", seq);
		params.put(time+"_file", file);
		params.put("ori", ori);
		params.put(
				"sign",
				CryptUtil.sign(params,ApiConfig.HTTP_KEY));
		Log.d("url:", ApiConfig.BASE_URL + ApiConfig.APIURL.URL_UPLOADREPLYALBUM+params.toString());
		client.post(
				ApiConfig.BASE_URL + ApiConfig.APIURL.URL_UPLOADREPLYALBUM,
				params, fileHandler);
	}



	/**
	 * 开始上传用户图片
	 * 
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
	public void beginUploadUserImage(String userId, String seq,
			File file, AsyncHttpResponseHandler fileHandler)  throws FileNotFoundException {
		if (!file.exists()) {
			throw new FileNotFoundException("file:" + file.getName()
					+ " not exists...");
		}
		
		RequestParams params = new RequestParams();
		String time = String.valueOf(System.currentTimeMillis());
		params.put("userId", userId);
		params.put("logo", file);
		params.put("t", time);
		params.put("seq", seq);

		params.put("sign",CryptUtil.sign(params,ApiConfig.HTTP_KEY));

		System.out.println("---" + params.toString());
		client.post(ApiConfig.BASE_URL + ApiConfig.APIURL.URL_UPLOADIMAGE,
				params, fileHandler);
	}
	
	/**
	 * 开始上传用户图片
	 * 
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
	public void beginUploadGroupImage(String gno, String seq,
			File file, AsyncHttpResponseHandler fileHandler)  throws FileNotFoundException {
		if (!file.exists()) {
			throw new FileNotFoundException("file:" + file.getName()
					+ " not exists...");
		}
		
		RequestParams params = new RequestParams();
		String time = String.valueOf(System.currentTimeMillis());
		params.put("gno", gno);
		params.put("groupimage", file);
		params.put("t", time);
		params.put("seq", seq);

		params.put("sign", CryptUtil.md5("gno=" + gno +"seq=" + seq + "&t=" + time
				+ "&key=" + ApiConfig.HTTP_KEY));

		System.out.println("---" + params.toString());
		client.post(ApiConfig.BASE_URL + ApiConfig.APIURL.URL_UPLOAD_GROUPIMAGE,
				params, fileHandler);
	}
	

	
	/**
	 * 回复帖子  可上传语音文件
	 * 
	 * @param file
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
	/*public void replyTopicUploadVoice(File file,PostsReplyTopicParam replyparams,int recodeTime,
			AsyncHttpResponseHandler fileHandler) throws FileNotFoundException {
		
		RequestParams params = new RequestParams();
		String time = String.valueOf(System.currentTimeMillis());
		
		String content = replyparams.getContent();
		if (file!=null && file.exists()) { //有语音文件
			params.put(time+"_file", file);
			content = content +(InterestConstant.URL_VOICE_PROTOCOL+recodeTime);
		}
		
		params.put("postsId", replyparams.getPostsId());
		params.put("content", content);
		params.put("postsUserId", replyparams.getPostsUserId());
		params.put("userId", replyparams.getUserId());
		params.put("t", time);
		
		params.put(
				"sign",
				CryptUtil.md5("content=" +content+"&postsId=" + replyparams.getPostsId() +"&postsUserId=" + replyparams.getPostsUserId() +"&t="
						+ time + "&userId=" + replyparams.getUserId() + "&key=" + ApiConfig.HTTP_KEY ));
		Log.d("url:", ApiConfig.BASE_URL + ApiConfig.APIURL.URL_POSTS_REPLYTOPIC+params.toString());
		client.post(
				ApiConfig.BASE_URL + ApiConfig.APIURL.URL_POSTS_REPLYTOPIC,
				params, fileHandler);
	}*/
	
	
	/**
	 * 回复评论  可上传语音文件
	 * 
	 * @param file
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
/*	public void replyCommentUploadVoice(File file,PostsReplyCommentParam replyparams,int recodeTime,
			AsyncHttpResponseHandler fileHandler) throws FileNotFoundException {
		
		RequestParams params = new RequestParams();
		String time = String.valueOf(System.currentTimeMillis());
		
		String content = replyparams.getContent();
		if (file!=null && file.exists()) { //有语音文件
			params.put(time+"_file", file);
			content = content +(InterestConstant.URL_VOICE_PROTOCOL+recodeTime);
		}
		
		params.put("content", content);
		params.put("postsId", replyparams.getPostsId());
		params.put("postsUserId", replyparams.getPostsUserId());
		params.put("replyId", replyparams.getReplyId());
		params.put("replyUserId", replyparams.getReplyUserId());
		params.put("userId", replyparams.getUserId());
		params.put("t", time);
		
		params.put(
				"sign",
				CryptUtil.md5("content=" +content+"&postsId=" + replyparams.getPostsId() +"&postsUserId=" + replyparams.getPostsUserId()+
						"&replyId=" + replyparams.getReplyId()+"&replyUserId=" + replyparams.getReplyUserId()+
						"&t="+ time + "&userId=" + replyparams.getUserId() + "&key=" + ApiConfig.HTTP_KEY ));
		Log.d("url:", ApiConfig.BASE_URL + ApiConfig.APIURL.URL_POSTS_REPLYTOPIC+params.toString());
		client.post(
				ApiConfig.BASE_URL + ApiConfig.APIURL.URL_POSTS_REPLYCOMMNET,
				params, fileHandler);
	}*/
	
	/**
	 * 回复评论  可上传语音文件
	 * 
	 * @param file
	 * @param fileHandler
	 * @throws FileNotFoundException
	 */
/*	public void createGroupUploadImage(File file,GroupCreateParam createParam,
			AsyncHttpResponseHandler fileHandler) throws FileNotFoundException {
		
		RequestParams params = new RequestParams();
		String time = String.valueOf(System.currentTimeMillis());
		
		params.put(time+"_file", file);
		
		params.put("gname", createParam.getGname());
		params.put("location", createParam.getLocation());
		params.put("lon", createParam.getLon());
		params.put("lat", createParam.getLat());
		params.put("remark", createParam.getRemark());
		params.put("userId", createParam.getUserId());
		params.put("t", time);
		
		
		params.put(
				"sign",
				CryptUtil.md5("gname=" +createParam.getGname()+"&lat=" + createParam.getLat() +"&location=" + createParam.getLocation()+
						"&lon=" + createParam.getLon()+"&remark=" + createParam.getRemark()+
						"&t="+ time + "&userId=" + createParam.getUserId() + "&key=" + ApiConfig.HTTP_KEY ));
		Log.d("url:", ApiConfig.BASE_URL + ApiConfig.APIURL.URL_GROUP_CREATE+params.toString());
		client.post(
				ApiConfig.BASE_URL + ApiConfig.APIURL.URL_GROUP_CREATE,
				params, fileHandler);
	}*/

}
